#include "favorismodel.h"
#include <QDebug>

favorismodel::favorismodel(QObject *parent) :
    QAbstractListModel(parent)
{
    QHash<int, QByteArray> roles;
    roles[DenominationRole] = "denomination";
    roles[CodeRole] = "code";

    setRoleNames(roles);
}

int favorismodel::rowCount(const QModelIndex &parent) const
{
    return mDatas.count();
}

QVariant favorismodel::data(const QModelIndex &index, int role) const
{

    if(role== DenominationRole)
    {
        QString rmName = mDatas.at(index.row()).value("denomination").toString();

        QString code =  mDatas.at(index.row()).value("code").toString();
        code = denomination.replace(rmName,"");


        return denomination;
    }

    if(role== CodeRole)
    {
        return mDatas.at(index.row()).value("code").toString();
    }


    return QVariant();

}

void MedicsModel::loadfavoris(const QString &filter)
{

    mDatas.clear();

    emit reset();

    QString q;

    if (filter.isEmpty())
        q = "SELECT code,denomination FROM Favoris";

        //q = "SELECT code,denomination,forme FROM CIS WHERE status = 'Commercialisée' ";

    else
        q="SELECT code,denomination FROM Favoris WHERE denomination LIKE '"+filter+"%'";


    QSqlQuery query(q); //la on peut definir la requette query ("SELECT dci FROM COMPO");
    while (query.next()) {
        beginInsertRows(QModelIndex(),0,0);
        mDatas.append(query.record());//remplir les donnees
        endInsertRows();
    }


    qDebug()<<query.lastError();

}



